package com.dog.basics.service.common.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Validator;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dog.basics.bean.dto.admin.common.CommonProblemClassAddDto;
import com.dog.basics.bean.dto.admin.common.CommonProblemClassEditDto;
import com.dog.basics.bean.dto.admin.common.CommonProblemClassSelectDto;
import com.dog.basics.bean.model.common.SysCommonProblem;
import com.dog.basics.bean.model.common.SysCommonProblemClass;
import com.dog.basics.bean.vo.admin.sys.CommonProblemClassPageListVo;
import com.dog.basics.bean.vo.admin.sys.CommonProblemClassPageVo;
import com.dog.basics.common.util.PageParam;
import com.dog.basics.dao.common.SysCommonProblemClassMapper;
import com.dog.basics.dao.common.SysCommonProblemMapper;
import com.dog.basics.security.util.SecurityUtils;
import com.dog.basics.service.common.IDogSysFileInfoService;
import com.dog.basics.service.common.ISysCommonProblemClassService;

import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 常见问题分类 服务实现类
 * </p>
 *
 * @author fengjukang
 * @since 2019-10-11
 */
@Service
@AllArgsConstructor
public class SysCommonProblemClassServiceImpl extends ServiceImpl<SysCommonProblemClassMapper, SysCommonProblemClass>
        implements ISysCommonProblemClassService {

    private final IDogSysFileInfoService fileInfoService;
    private final SysCommonProblemMapper syscommonproblemmapper;

    /**
     * 常见问题列表
     *
     * @param page
     * @param dto
     * @return
     * @author guonh
     */
    @Override
    public IPage<CommonProblemClassPageVo> getCommonProblemClass(PageParam page, CommonProblemClassSelectDto dto) {
        this.page(page, Wrappers.<SysCommonProblemClass>query().lambda()
                .eq(ObjectUtils.isNotNull(dto.getTitle()), SysCommonProblemClass::getTitle,
                        dto.getTitle()));
        List<SysCommonProblemClass> records = page.getRecords();
        //参数封装
        page.setRecords(records.stream().map(rec -> {
            CommonProblemClassPageVo commonProblemClassPageVo = new CommonProblemClassPageVo();
            BeanUtil.copyProperties(rec, commonProblemClassPageVo);
            //判断 -  图片查询 - 赋值
            if (ObjectUtils.isNotNull(rec.getProblemClassLogo()) && !rec.getProblemClassLogo()
                    .equals(Integer.valueOf(-1))) {
                fileInfoService.getFileInfoUrl(CollUtil.newArrayList(rec.getProblemClassLogo())).stream()
                        .forEach(file -> commonProblemClassPageVo.setProblemClassLogoUrl(file.getFilePath()));
            }
            return commonProblemClassPageVo;
        }).collect(Collectors.toList()));
        return page;
    }

    /**
     * 常见问题列表
     *
     * @param page
     * @param dto
     * @return
     * @author guonh
     */
    @Override
    public IPage<CommonProblemClassPageListVo> getCommonProblemClassPageList(Page page, CommonProblemClassSelectDto dto) {
        this.page(page, Wrappers.<SysCommonProblemClass>query().lambda()
                .eq(ObjectUtils.isNotNull(dto.getTitle()), SysCommonProblemClass::getTitle,
                        dto.getTitle()));
        List<SysCommonProblemClass> records = page.getRecords();

        Validator.validateNotNull(records, "暂无数据");

        page.setRecords(records.stream().map(problemClass -> BeanUtil.toBean(problemClass, CommonProblemClassPageListVo.class)).collect(Collectors.toList()));

        return page;
    }

    /**
     * 常见问题新增
     *
     * @param commonProblemClassAddDto
     * @return
     * @author guonh
     */
    @Override
    public Boolean addCommonProblemClass(CommonProblemClassAddDto commonProblemClassAddDto) {
        List<SysCommonProblemClass> list = this.list(Wrappers.<SysCommonProblemClass>query().lambda()
                .eq(SysCommonProblemClass::getTitle, commonProblemClassAddDto.getTitle()));
        Validator.validateTrue(CollUtil.isEmpty(list), "分类名称已存在");
        SysCommonProblemClass sysCommonProblemClass = new SysCommonProblemClass();

        BeanUtil.copyProperties(commonProblemClassAddDto, sysCommonProblemClass);
        sysCommonProblemClass.setCreateBy(SecurityUtils.getSysUser().getUserId());
        sysCommonProblemClass.setCreateTime(DateUtil.currentSeconds());
        Validator.validateTrue(this.save(sysCommonProblemClass), "常见问题添加失败");

        return Boolean.TRUE;
    }

    /**
     * 常见问题修改
     *
     * @param commonProblemClassEditDto
     * @return
     * @author guonh
     */
    @Override
    public Boolean editCommonProblemClass(CommonProblemClassEditDto commonProblemClassEditDto) {
        SysCommonProblemClass byId = this.getById(commonProblemClassEditDto.getId());
        Validator.validateNotNull(byId, "无此信息");
        BeanUtil.copyProperties(commonProblemClassEditDto, byId);
        byId.setUpdateBy(SecurityUtils.getSysUser().getUserId());
        byId.setUpdateTime(DateUtil.currentSeconds());
        Validator.validateTrue(this.updateById(byId), "常见问题修改失败");
        return Boolean.TRUE;
    }

    /**
     * 常见问题删除
     *
     * @param id
     * @return
     * @author guonh
     */
    @Override
    public Boolean delCommonProblemClass(Long id) {
        SysCommonProblemClass byId = this.getById(id);
        Validator.validateNotNull(byId, "无此信息");
        List<SysCommonProblem> list = syscommonproblemmapper.selectList(Wrappers.<SysCommonProblem>query().lambda().eq(SysCommonProblem::getProblemClassId, id));
        Validator.validateTrue(CollUtil.isEmpty(list), "无法删除");
        Validator.validateTrue(this.removeById(id), "常见问题删除失败");
        return Boolean.TRUE;
    }
}
